package com.bootdo.wx.service.impl;

import cn.binarywang.wx.miniapp.api.WxMaService;
import cn.binarywang.wx.miniapp.bean.WxMaSubscribeMessage;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.IdUtil;
import com.bootdo.common.config.WxMaConfiguration;
import com.bootdo.common.config.WxMaProperties;
import com.bootdo.common.utils.DateUtils;
import com.bootdo.common.utils.R;
import com.bootdo.wx.constant.Constants;
import com.bootdo.wx.dao.CmsNoticeDao;
import com.bootdo.wx.dao.MemberApplyDao;
import com.bootdo.wx.dao.MemberUserDao;
import com.bootdo.wx.domain.CmsNoticeDO;
import com.bootdo.wx.domain.MemberApplyDO;
import com.bootdo.wx.domain.MemberUserDO;
import com.bootdo.wx.service.MemberApplyService;
import me.chanjar.weixin.common.error.WxErrorException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;



@Service
public class MemberApplyServiceImpl implements MemberApplyService {
	@Autowired
	private MemberApplyDao memberApplyDao;
	@Autowired
	private MemberUserDao memberUserDao;
	@Autowired
	private WxMaProperties properties;
	@Autowired
	private CmsNoticeDao cmsNoticeDao;

	
	@Override
	public MemberApplyDO get(String id){
		return memberApplyDao.get(id);
	}
	
	@Override
	public List<MemberApplyDO> list(Map<String, Object> map){
		return memberApplyDao.list(map);
	}
	
	@Override
	public int count(Map<String, Object> map){
		return memberApplyDao.count(map);
	}
	
	@Override
	public int save(MemberApplyDO memberApply){
		return memberApplyDao.save(memberApply);
	}
	
	@Override
	public int update(MemberApplyDO memberApply){
		return memberApplyDao.update(memberApply);
	}
	
	@Override
	public int remove(String id){
		return memberApplyDao.remove(id);
	}
	
	@Override
	public int batchRemove(String[] ids){
		return memberApplyDao.batchRemove(ids);
	}

    @Override
    public List<MemberApplyDO> likeList(Map<String, Object> map){
        return memberApplyDao.likeList(map);
    }

    @Override
    public R applyUpgrade(Map<String, Object> params) {
		params.put("delFlag","0");
		List<MemberApplyDO> list = memberApplyDao.list(params);
		if(list.size()>0){
			if(list.get(0).getStatus()==0){
				return R.ok(1,"审核中,请耐心等待");
			}
		}
		MemberApplyDO memberApplyDO = new MemberApplyDO();
		memberApplyDO.setDelFlag(0);
		memberApplyDO.setCaAt(new Date());
		memberApplyDO.setUserId(MapUtil.getStr(params,"userId"));
		memberApplyDO.setId(IdUtil.simpleUUID());
		memberApplyDO.setStatus(0);
		memberApplyDao.save(memberApplyDO);

		return R.ok(0,"申请成功");


    }

    @Override
    public R shen(MemberApplyDO memberApplyDO) {
		MemberApplyDO memberApplyDO1 = memberApplyDao.get(memberApplyDO.getId());

		String result = memberApplyDO.getStatus()==1?"通过":"拒绝";
		MemberUserDO memberUserDO = memberUserDao.get(memberApplyDO1.getUserId());
		if(memberApplyDO.getStatus()>0){
			try {
				//发送订阅消息
				final WxMaService wxService = WxMaConfiguration.getMaService(properties.getConfigs().get(0).getAppid());
				WxMaSubscribeMessage subscribeMessage = new WxMaSubscribeMessage();
				subscribeMessage.setToUser(memberUserDO.getWechatOpenId());
				subscribeMessage.setTemplateId(Constants.mesage.MODE_AUDIT_RESULT);
				subscribeMessage.setPage(Constants.mesage.address.ADDRESS_INDEX_INDEX);
				List<WxMaSubscribeMessage.Data> list = new ArrayList<>();
				for (int i = 0; i < 4; i++) {
					WxMaSubscribeMessage.Data data = new WxMaSubscribeMessage.Data();
					switch (i) {
						case 0:
							data.setName("name7");
							data.setValue(memberUserDO.getName());
							break;
						case 1:
							data.setName("date1");
							data.setValue(DateUtil.format(new Date(), DateUtils.DATE_TIME_PATTERN));
							break;
						case 2:
							data.setName("thing2");

							data.setValue(result);
							break;
						case 3:
							data.setName("thing14");
							data.setValue("升级申请");
							break;
						default:
							break;
					}
					list.add(data);


				}
				subscribeMessage.setData(list);
				wxService.getMsgService().sendSubscribeMsg(subscribeMessage);//发送订阅消息

			} catch (WxErrorException e) {
				e.printStackTrace();
			}
		}



			if(memberApplyDO.getStatus()==1){

				if(memberApplyDO1.getStatus()==1){
					return R.error("已经通过,请刷新页面");
				}
				memberApplyDO1.setStatus(1);
				memberApplyDO1.setUpAt(new Date());
				memberApplyDao.update(memberApplyDO1);
				//MemberUserDO memberUserDO = memberUserDao.get(memberApplyDO1.getUserId());
				if(memberUserDO!=null){
					if(memberUserDO.getLevel()==3){
						return R.error("已经是合伙人,无法升级");
					}
					memberUserDO.setLevel(memberUserDO.getLevel()+1);
					memberUserDO.setUpdateAt(new Date());
					memberUserDao.update(memberUserDO);

				}else{
					return R.error("用户信息有误");
				}

			}else if(memberApplyDO.getStatus()==2){
				//MemberApplyDO memberApplyDO1 = memberApplyDao.get(memberApplyDO.getId());
				memberApplyDO1.setStatus(2);
				memberApplyDO1.setUpAt(new Date());
				memberApplyDao.update(memberApplyDO1);

			}

			//往平台公告表加入数据
			CmsNoticeDO cmsNoticeDO = new CmsNoticeDO();
			cmsNoticeDO.setId(IdUtil.simpleUUID());
			cmsNoticeDO.setContent("您申请升级结果为:"+result+"如有疑问,请联系客服!");
			cmsNoticeDO.setDelFlag(0);
			cmsNoticeDO.setCaAt(new Date());
			cmsNoticeDO.setTitle("申请升级");
			cmsNoticeDO.setUserId(memberUserDO.getId());
			cmsNoticeDao.save(cmsNoticeDO);
			return R.ok();



		}



}
